package com.shangxia.leetcode.greedyAlgorithm;

import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @ProjectName: leetcode
 * @ClassName: EraseOverlapIntervals435
 * @Time: 2020/12/6
 * @Author: zhouy
 * @Description: 435. 无重叠区间
 * @Experience:
 */

@Slf4j
public class EraseOverlapIntervals435 {
    public static void main(String[] args) {
        int[][] intervals = new int[][]{new int[]{1, 2}, new int[]{3, 4}, new int[]{1, 2}, new int[]{2, 3}};
        System.out.println(eraseOverlapIntervals(intervals));
    }

    public static int eraseOverlapIntervals(int[][] intervals) {
        Arrays.sort(intervals, (o1, o2) -> {
            if (o1[0] > o2[0]) {
                return 1;
            } else if (o1[0] == o2[0]) {
                if (o1[1] > o2[1]) {
                    return 1;
                }
            }
            return -1;
        });
        int count = 0;
        for (int i = 1; i < intervals.length; i++) {
            if (intervals[i][0] < intervals[i - 1][1]) {
                count++;
                if (intervals[i][1] >= intervals[i - 1][1]){
                    intervals[i] = intervals[i - 1];
                }
            }
        }
        return count;
    }
}
